home *** CD-ROM | disk | FTP | other *** search
/ Loadstar 128 #31 / q31.d81 / t.random demo < prev    next >
Encoding:
Text File  |  1992-01-01  |  9.7 KB  |  199 lines

  1.  
  2.  
  3.                           R A N D O M   2 - 2 5 4
  4.                                       
  5.                       Program and Text by Bob Markland                      
  6.                                       
  7.                                       
  8.     When we shuffle a deck of cards or watch a horse race we don't give
  9. much thought to the non-repeating random numbers reflected in the outcome.
  10. Even a tree shedding its leaves in the fall illustrates this principal.
  11. There is no certainty as to which leaf will fall first, in what order each
  12. will fall, and which will be last.  But since no leaf can fall twice this
  13. is a non-repeating random number sequence.
  14.  
  15.     As programmers we must take non-repeating random numbers into account
  16. so that our programs accurately reflect the real world. It is incumbent
  17. upon us to realistically distribute numbers for a card shuffle or game
  18. simulation. Presentation of quiz questions or game levels in a non-
  19. repeating random order may sometimes be appropriate.  Sooner or later
  20. you'll find need for RANDOM 2-254.
  21.  
  22.     By definition, non-repeating random numbers are a series of sequential
  23. numbers selected in random order.  For example: the series 1-2-3 could be
  24. picked in the order 2-3-1 or 3-1-2 or 1-3-2, etc.  To simulate a standard
  25. deck of cards you need 52 consecutive numbers, usually 1 through 52.
  26.  
  27.     There are several BASIC routines which use the Commodore RND function
  28. to accomplish this end.  However, they tend to be painfully slow and I have
  29. found some which unexpectedly grind to a halt near the end while trying to
  30. find the last number or two which has not yet been used.
  31.  
  32.     RANDOM 2-254 is a small machine language program capable of simulating
  33. a card shuffle, for example, in less than a quarter of a second.  It
  34. derives its random values from the CIA chip, which is said to provide a
  35. more statistically random number selection than the Commodore RND function.
  36. In addition to being quick it is also very versatile:  You can specify a
  37. series of 2 to 254 numbers.  The program can be loaded to many areas of
  38. free RAM in BANK 0 without modification.  It's also compatible with other
  39. M/L routines in both 128 and 64 modes.
  40.  
  41.     The demo program which runs from LOADSTAR demonstrates three areas
  42. where RANDOM 2-254 is useful.  It will be most beneficial to print a hard
  43. copy of this text and refer to it as you progress through the demo. You
  44. should also break out of the demo with RUN/STOP then LIST to learn how the
  45. routines utilize RANDOM 2-254 in different circumstances.
  46.  
  47.  
  48.  GENERAL
  49.  -------
  50.  
  51.     Lines 30 and 40 of the demo select the desired load address for RANDOM
  52. 2-254 and load it.  The load address is contained in the variable ML and
  53. may be any address in BANK 0 free RAM as indicated below.  Your particular
  54. program and its use of other M/L routines will dictate the best place to
  55. put RANDOM 2-254.  This can include any unused portion of RAM from 2816
  56. ($0B00) to 7167 ($1BFF). Any area of RAM between the end of your BASIC
  57. program and 16383 ($3FFF) may be utilized.  If you have used the GRAPHIC
  58. commands to move your BASIC program upward to $4000 and are not otherwise
  59. using the area for graphics, any part of the RAM from 7168 ($1C00) to 16383
  60. ($3FFF) is available for RANDOM 2-254.
  61.  
  62.     The memory required is 95 bytes for RANDOM 2-254 plus 1 byte for each
  63. number to be randomized.  Thus, 254 numbers require 349 bytes of memory.
  64.  
  65.     Usually subroutines are loaded into memory at even multiples of 256.
  66. However, for the purpose of illustration, you will notice in line 30 that
  67. ML=2829.  You can attach RANDOM 2-254 to the end of any other M/L routine
  68. if you are proficient with an M/L monitor.  RANDOM 2-254 can also be called
  69. as a subroutine from within your own M/L program or routine
  70.  
  71.  FENDER'S NOTE: Usually you can assume that the end address of a machine
  72. language routine is the last byte it will be using, but not always.  RANDOM
  73. 2-254 itself is a program that uses some memory directly after the end of
  74. the routine.  If you place something directly after a machine language
  75. routine and the routine all of a sudden stops working correctly, suspect
  76. that the routine uses that area for something and move whatever you placed
  77. there.
  78.  
  79.  
  80.  CARD GAMES
  81.  ----------
  82.  
  83.     Lines 70-120 of the demo illustrate one method of defining a standard
  84. deck of 52 cards.  In order to display 52 cards on the screen
  85. simultaneously, miniature cards are created with a custom font.  Using this
  86. same font, and with a few simple changes in the PRINT statements
  87. demonstrated in lines 310-320 you can easily display the large cards common
  88. to LOADSTAR.
  89.  
  90.     In this demo the card back is not represented.  It is customary to
  91. utilize the Zero (0) element of the CN( ) array to specify a card back.
  92.  
  93.     Although it is beyond the scope of this article, it is not too
  94. difficult to create non-standard card decks.  By altering the arrays which
  95. represent the cards and their values you can create any deck -- 48 card
  96. Pinochle, 32 card Clobber, 104 card Canasta, 208 card Baccarat, multiple
  97. decks for casino Blackjack, an UNO(r) deck, or even Tarot cards.
  98.  
  99.     The first demo displays 52 cards as they would appear when you first
  100. open a factory fresh deck.  Press [S] to shuffle and re-display the cards.
  101.  
  102.     Line 210 sets the variable N to 52, then passes the address to which
  103. you have loaded RANDOM 2-254 and tells it how many numbers to randomize.
  104.  
  105.     Line 220 randomizes the series of numbers. NOTE: SYSML is the ONLY
  106. command on the line necessary to RANDOM 2-254.  The remaining variables in
  107. this line calculate the elapsed time required to complete the shuffle for
  108. the purpose of demonstration.  In your own program, simply SYSML once each
  109. time you need to shuffle.
  110.  
  111.     RANDOM 2-254 generates a table of the randomized numbers.  Line 230
  112. shows how to access the random sequence.  The variable CN represents the
  113. definition of each card in its original sequence.  The variable NU is the
  114. number of the card to be dealt.  When NU=1 the top card on the deck is
  115. specified with CN=PEEK(ML+94+NU).  When NU=2 the second card from the top
  116. of the shuffled deck is returned in CN, etc.
  117.  
  118.  
  119.  QUIZZES or LEVELS
  120.  -----------------
  121.  
  122.     To enhance learning it is desirable to present test questions in a
  123. different order each time the test is taken to prevent memorization of the
  124. sequence and rote answers.  There are also applications in arcade type
  125. games where you may wish to randomly select different screens in a non-
  126. repeating order.
  127.  
  128.     Line 420 sets the parameters for 30 questions or levels.  For 10, 50 or
  129. 200 questions/levels, adjust N accordingly.
  130.  
  131.     As above, SYSML is the only relevant statement in line 430.  Use SYSML
  132. once at any time you wish to change the random order.
  133.  
  134.     In your own program you could use DATA statements to list 30 questions
  135. and 4 multiple-choice answers for each, then READ them into a 2-dimensional
  136. array.  Use an incremented variable such as NU, as shown in line 440, to
  137. step through the random number table.  In this case QN=PEEK(ML+94+NU) will
  138. return the number of the question/level to present next.
  139.  
  140.  
  141.  KENO, BINGO, ETC.
  142.  -----------------
  143.  
  144.     Several games of chance, including Keno, Bingo and Lotteries utilize
  145. non-repeating random numbers, as do several other applications.
  146.  
  147.     The third section of the demo illustrates how RANDOM 2-254 is used in
  148. Keno.  The screen displays the 80 possible numbers in the game.  After
  149. randomizing, the first 20 numbers are used to determine the outcome.
  150.  
  151.     A Keno layout consists of numbers from 1 to 80 as indicated by the
  152. variable N in line 610.  For Bingo, which utilizes 99 possible numbers, set
  153. N=99.  Once again, pass the load address for RANDOM 2-254 and tell the
  154. program how many numbers to randomize, using the indicated POKES.
  155.  
  156.     The object of Keno is for the player to pick from 1 to 10 numbers he
  157. believes will also be picked by the "ball machine" (computer).  The "ball
  158. machine" then picks 20 random numbers and if the player makes the required
  159. number of matches he wins.
  160.  
  161.     For the Keno simulation set N=80 then SYSML.  The first 20 numbers
  162. selected become relevant and the remainder are discarded.  Thus, NU is
  163. incremented from 1 to 20 to return values in X, which would then be
  164. compared to the player's picks.  For Bingo: set N=99, SYSML, then increment
  165. NU to return values in X until a Bingo occurs.
  166.  
  167.  
  168.  OTHER USES
  169.  ----------
  170.  
  171.     You will no doubt think of many other uses for non-repeating random
  172. numbers beyond those discussed here.  Beyond simulations, there are
  173. numerous real-world applications.
  174.  
  175.     In elementary school one of the most humiliating experiences for
  176. children who lack coordination is the choosing of sides for team sports.
  177. The same people are always the last ones picked.  Allowing a human to pick
  178. teams comes with built-in bias.  Instead, for a class of 30, assign each
  179. person a number, then use RANDOM 2-254 in your program to randomize the 30
  180. numbers.  The first 15 numbers picked are on one team and the second 15 are
  181. on the other.
  182.  
  183.     If you happen to be looking for a science project consider software
  184. which purports to pick winning Lottery numbers.  Ask any statistician -- no
  185. matter which numbers were picked last week, last month, or last year
  186. (barring fraud or mechanical irregularity) every number has an equal chance
  187. of being picked the next time the game is played.
  188.  
  189.     To prove that Lottery picking software is little more than a scam, use
  190. it to pick "lucky" numbers, then write a program which sets up RANDOM 2-254
  191. to pick random numbers using the same criteria.  With a statistically
  192. significant number of repetitions, you can easily prove that numbers picked
  193. at random are just as likely to win as those picked by Lottery software.
  194.  
  195.     Experiment with RANDOM 2-254 for your own applications.  Or better yet,
  196. use it to develop a program that you can sell to LOADSTAR.
  197.  
  198.                     \\\\\  R - Run   RETURN - Menu  \\\\\
  199.